
//
//  MeetingViewController.m
//  Qihuo
//
//  Created by Vencoo on 13-9-25.
//  Copyright (c) 2013年 Vencoo. All rights reserved.
//
#define viewWidth 984
#define viewHeight 624
#define inter 10
#import "MeetingView.h"
#import "MeetingViewController.h"
#import "CalenderView.h"
#import "NSDate+Calendar.h"
#import "CaculateDate.h"
#import "AddMeetViewController.h"
#import "BlackbackgroundView.h"
#import <CoreFoundation/CoreFoundation.h>
@interface MeetingViewController ()
{
    UITableView *yearTable;
    UITableView *monthTable;
    UITableView *roomTable;
    
    UIButton *yearBtn;
    UIButton *monthBtn;
    UIButton *topButton;
    
    NSDate *today;
    NSArray *places;
    int row;
    NSMutableArray *yearArray;
    NSMutableArray *monthArray;
    NSMutableArray *dayLabelArray;
    NSMutableArray *meetViews;
    CalenderView *calenderRect;
    NSDate *date;
    UILabel *nameLabel;
    
    NSMutableArray *eventFromDates;
    NSMutableArray *eventToDates;
    NSMutableArray *eventNorms;
}
@end

@implementation MeetingViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self)
    {
        
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self addRightButton];
    [self initUI];
    [self addCalender];
    [self netRequest:@""];
    [self initData];
//    [self addSeeMeeting];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - program
- (void)initData
{
    eventFromDates = [NSMutableArray array];
    eventToDates = [NSMutableArray array];
    eventNorms = [NSMutableArray array];
    meetViews = [NSMutableArray array];
}
- (void)addSeeMeeting
{
    UIImage *inputImage = [UIImage imageNamed:@"灰底输入框"];
    BlackbackgroundView *blackV = [[BlackbackgroundView alloc]initWithFrame:CGRectMake(0, 0, scrennWidth, screenHeight)];
    blackV.userInteractionEnabled = YES;
    [self.view addSubview:blackV];
    
    UILabel *titleLb = [UILabel new];
    titleLb.text = @"会议记录查询";
    titleLb.font = [UIFont systemFontOfSize:35];
    titleLb.backgroundColor = [UIColor clearColor];
    CGSize sz = [titleLb.text sizeWithFont:titleLb.font constrainedToSize:CGSizeMake(MAXFLOAT, 40)];
    titleLb.frame = CGRectMake((blackV.frame.size.width - sz.width)/2, 80, sz.width, sz.height);
    [blackV addSubview:titleLb];

    UIImage *backgroundimage = [UIImage imageNamed:@"37F底框"];
    UIImage *whiteimage = [UIImage imageNamed:@"弹出出灰底"];
    UIScrollView *_whiteView = [[UIScrollView alloc]init];
    _whiteView.frame = CGRectMake((backgroundimage.size.width - whiteimage.size.width)/2, 79, whiteimage.size.width, whiteimage.size.height);
    [blackV addSubview:_whiteView];

    UILabel *activeNameLabel = [UILabel new];
    [activeNameLabel setText:@"活动名称："];
    [activeNameLabel setFont:[UIFont systemFontOfSize:character]];
    CGSize acticeSize = [activeNameLabel.text sizeWithFont:activeNameLabel.font constrainedToSize:CGSizeMake(MAXFLOAT, 40)];
    activeNameLabel.frame = CGRectMake(interval111, interval111+titleLb.frame.size.height, acticeSize.width, acticeSize.height);
    [_whiteView addSubview:activeNameLabel];
    
    UITextField *activeNameText = [[UITextField alloc]initWithFrame:CGRectMake(activeNameLabel.frame.size.width + interval111, activeNameLabel.frame.origin.y, tableViewHeight , activeNameLabel.frame.size.height)];
    activeNameText.contentVerticalAlignment = UIViewContentModeCenter;
    [activeNameText setBackground:inputImage];
    activeNameText.enabled = NO;
    [_whiteView addSubview:activeNameText];
    
    UILabel *acticeNumberLb = [UILabel new];
    acticeNumberLb.text = @"活动编号：";
    acticeNumberLb.font = [UIFont systemFontOfSize:character];
    acticeSize = [acticeNumberLb.text sizeWithFont:acticeNumberLb.font constrainedToSize:CGSizeMake(MAXFLOAT, 40)];
    acticeNumberLb.frame = CGRectMake(activeNameText.frame.origin.x + activeNameText.frame.size.width + interval111, activeNameText.frame.origin.y, acticeSize.width, acticeSize.height);
    [_whiteView addSubview:acticeNumberLb];
    
    UITextField *activeNumberText = [[UITextField alloc]initWithFrame:CGRectMake(acticeNumberLb.frame.origin.x + acticeNumberLb.frame.size.width, acticeNumberLb.frame.origin.y, 250, acticeSize.height)];
    activeNumberText.contentVerticalAlignment = UIViewContentModeCenter;
    activeNumberText.enabled = 0;
    [activeNumberText setBackground:inputImage];
    [_whiteView addSubview:activeNumberText];
}
- (void)calculate
{
    yearArray = [NSMutableArray array];
    for (int year = 2001; year <= 2050; year++) {
        [yearArray addObject:[NSString stringWithFormat:@"%d",year]];
    }
    monthArray = [NSMutableArray array];
    for (int month = 1; month <= 12; month++) {
        [monthArray addObject:[NSString stringWithFormat:@"%d",month]];
    }
    
}
- (void)initUI
{
    places = [NSArray arrayWithObjects:@"全部",@"国际会议厅",@"多功能厅",@"6F视听室",@"37F西餐厅",@"37F贵宾室",@"804会议室",@"801",@"3302",@"3408",@"3604",@"37F西餐厅",@"37F贵宾室",@"6F博物馆",@"4F交易大厅",@"4F计算机房",@"其他",nil];
    
    dayLabelArray = [NSMutableArray array];
    if (_iOS7) {
        calenderRect = [[CalenderView alloc]initWithFrame:CGRectMake(20, 120, 984, 624)];
    }else{
        calenderRect = [[CalenderView alloc]initWithFrame:CGRectMake(20, 100, 984, 624)];
    }
    
    calenderRect.backgroundColor = [UIColor whiteColor];
    calenderRect.userInteractionEnabled = YES;
    [self.view addSubview:calenderRect];
    self.title = @"日历";
    today = [NSDate date];
    [self calculate];
    
    
//    UIGestureRecognizer *gest = [[UIGestureRecognizer alloc]initWithTarget:self action:@selector(Nslog:)];
//    [calenderRect addGestureRecognizer:gest];
    
    UILabel *topLabel = [UILabel new];
    topLabel.text = @"名称：";
    [topLabel setFont:[UIFont systemFontOfSize:30]];
    CGSize size = [topLabel.text sizeWithFont:topLabel.font constrainedToSize:CGSizeMake(MAXFLOAT, 40)];
    topLabel.backgroundColor = [UIColor clearColor];
    topLabel.textColor = [UIColor whiteColor];
    if (_iOS7) {
        topLabel.frame = CGRectMake(40, 69, size.width, size.height);
    }
    else
    {
        topLabel.frame = CGRectMake(40, 49, size.width, size.height);
    }
    [self.view addSubview:topLabel];
    
    UIImage *backgroundImage = [UIImage imageNamed:@"日历下拉底框"];
    UIImageView *backgroundView = [[UIImageView alloc]initWithImage:backgroundImage];
    [backgroundView setFrame:CGRectMake(topLabel.frame.origin.x + topLabel.frame.size.width, topLabel.frame.origin.y, backgroundImage.size.width, backgroundImage.size.height)];
    backgroundView.userInteractionEnabled = YES;
    [self.view addSubview:backgroundView];
    
    UIImage *rectImage = [UIImage imageNamed:@"三角-底"];
    //    UIImageView *rectView = [[UIImageView alloc]initWithImage:rectImage];
    //    rectView.frame = CGRectMake(backgroundImage.size.width - rectImage.size.width, 0, rectImage.size.width, rectImage.size.height);
    //    rectView.userInteractionEnabled = YES;
    //    [backgroundView addSubview:rectView];
    
    nameLabel = [UILabel new];
    nameLabel.text = [places firstObject];
    nameLabel.font = [UIFont systemFontOfSize:30];
    nameLabel.frame = CGRectMake(rectImage.size.width/2, (backgroundImage.size.height - size.height)/2, backgroundImage.size.width - rectImage.size.width, size.height);
    nameLabel.backgroundColor = [UIColor clearColor];
    nameLabel.textColor = [UIColor whiteColor];
    [backgroundView addSubview:nameLabel];
    
    topButton = [UIButton buttonWithType:UIButtonTypeCustom];
    topButton.frame = CGRectMake(nameLabel.frame.size.width,0,rectImage.size.width,rectImage.size.height);
    [topButton setImage:[UIImage imageNamed:@"下拉-右框常态"] forState:UIControlStateNormal];
    [topButton addTarget:self action:@selector(room:) forControlEvents:UIControlEventTouchUpInside];
    
    [backgroundView addSubview:topButton];
    
    UIImage *laImage = [UIImage imageNamed:@"三角日历右"];
    _yearLabel = [UILabel new];
    _yearLabel.text = [NSString stringWithFormat:@"%u年", today.year];
    _yearLabel.backgroundColor = [UIColor clearColor];
    [_yearLabel setFont:[UIFont systemFontOfSize:42]];
    size = [_yearLabel.text sizeWithFont:_yearLabel.font constrainedToSize:CGSizeMake(MAXFLOAT, 40)];
    _yearLabel.frame = CGRectMake(237, 10 , size.width, size.height);
    
    yearBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    yearBtn.frame = CGRectMake(_yearLabel.frame.size.width + _yearLabel.frame.origin.x, _yearLabel.frame.origin.y + (_yearLabel.frame.size.height - laImage.size.height)/2, laImage.size.width, laImage.size.height);
    [yearBtn setImage:laImage forState:UIControlStateNormal];
    [yearBtn addTarget:self action:@selector(yearClick:) forControlEvents:UIControlEventTouchUpInside];
    yearBtn.backgroundColor = [UIColor clearColor];
    [calenderRect addSubview:yearBtn];
    [calenderRect addSubview:_yearLabel];
    
    _monthLabel = [UILabel new];
    _monthLabel.text = [NSString stringWithFormat:@"%u月",today.month];
    _monthLabel.backgroundColor = [UIColor clearColor];
    [_monthLabel setFont:[UIFont systemFontOfSize:42]];
    size = [_monthLabel.text sizeWithFont:_monthLabel.font constrainedToSize:CGSizeMake(MAXFLOAT, 40)];
    _monthLabel.frame = CGRectMake(602, _yearLabel.frame.origin.y, size.width, size.height);
    
    monthBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    monthBtn.frame = CGRectMake(_monthLabel.frame.size.width + _monthLabel.frame.origin.x, yearBtn.frame.origin.y, laImage.size.width, laImage.size.height);
    [monthBtn addTarget:self  action:@selector(monthClick:) forControlEvents:UIControlEventTouchUpInside];
    [monthBtn setImage:laImage forState:UIControlStateNormal];
    monthBtn.backgroundColor = [UIColor clearColor];
    [calenderRect addSubview:monthBtn];
    [calenderRect addSubview:_monthLabel];
    
    yearTable = [[UITableView alloc]initWithFrame:CGRectMake(_yearLabel.frame.origin.x, _yearLabel.frame.size.height + _yearLabel.frame.origin.y, _yearLabel.frame.size.width, 420) style:UITableViewStylePlain];
    yearTable.dataSource = self;
    yearTable.delegate = self;
    [calenderRect addSubview:yearTable];
    yearTable.hidden = YES;
    
    monthTable = [[UITableView alloc]initWithFrame:CGRectMake(_monthLabel.frame.origin.x, _monthLabel.frame.size.height + _monthLabel.frame.origin.y, _monthLabel.frame.size.width, 420) style:UITableViewStylePlain];
    monthTable.dataSource = self;
    monthTable.delegate = self;
    [calenderRect addSubview:monthTable];
    monthTable.hidden = YES;
    
    UIImage *roomImage = [UIImage imageNamed:@"日历下拉-底"];
    roomTable = [[UITableView alloc]initWithFrame:CGRectMake(topLabel.frame.origin.x + topLabel.frame.size.width ,  topLabel.frame.origin.y + backgroundImage.size.height, roomImage.size.width,roomImage.size.height) style:UITableViewStylePlain];
    roomTable.backgroundView = [[UIImageView alloc]initWithImage:roomImage];
    roomTable.backgroundColor = [UIColor clearColor];
    roomTable.dataSource = self;
    roomTable.delegate = self;
    [self.view addSubview:roomTable];
    roomTable.hidden = YES;
    
    calenderRect.meett = self;
    date = [NSDate date];
    NSArray *weekdays = @[@"星期日",@"星期一",@"星期二",@"星期三",@"星期四",@"星期五",@"星期六"];
    for (int i = 0; i<7 ;i++) {
        UILabel *label = [UILabel new];
        label.text = [weekdays objectAtIndex:i];
        label.font = [UIFont systemFontOfSize:24];
        label.textColor = [UIColor whiteColor];
        label.backgroundColor = [UIColor clearColor];
        size = [label.text sizeWithFont:label.font constrainedToSize:CGSizeMake(MAXFLOAT, 40)];
        label.frame = CGRectMake(inter*2 + (viewWidth - inter*4)/7 * i, _yearLabel.frame.size.height + inter *2, (viewWidth - inter*4)/7, inter * 5);
        label.textAlignment = NSTextAlignmentCenter;
        [calenderRect addSubview:label];
    }
    
}
- (void)addCalender
{
    CaculateDate *cal = [CaculateDate new];
    date = [NSDate getDateWithString:[NSString stringWithFormat:@"%04d-%02d",[_yearLabel.text intValue] ,[_monthLabel.text intValue]] DateFormat:@"yyyy-MM"];

    switch ([cal weekNumber:date]) {
        case 4:
        {
                for (NSString *day in [cal dayInMonth:date])
            {
                date = [NSDate getDateWithString:[NSString stringWithFormat:@"%04d-%02d-%02d",[_yearLabel.text intValue] ,[_monthLabel.text intValue],[day intValue]] DateFormat:@"yyyy-MM-dd"];

                CalenderBtn *button = [[CalenderBtn alloc]initWithFrame:CGRectMake((date.weekday - 1)*(viewWidth - inter * 4)/7 + inter * 2, (int)(((date.day+[cal weekdayOfFirstDay:date]-2)/7)*(viewHeight - _monthLabel.frame.size.height - inter *9)/5+_monthLabel.frame.size.height + inter *7), (viewWidth - inter * 4)/7-2, (viewHeight - _monthLabel.frame.size.height - inter *9)/5-4)];
                button.backgroundColor = [UIColor clearColor];
                button.tag = [day intValue];
                [calenderRect addSubview:button];
                [dayLabelArray addObject:button];
                
                UILabel *label = [UILabel new];
                if (date.year == [NSDate date].year && date.month == [NSDate date].month && date.day == [NSDate date].day)
                {
                    label.backgroundColor = [UIColor yellowColor];
                }
                else
                {
                    label.backgroundColor = [UIColor clearColor];
                }
                if (date.weekday == 1 ||date.weekday == 7)
                {
                    label.textColor = [UIColor redColor];
                }
                else
                {
                    label.textColor = [UIColor blackColor];
                }
                label.text = day;
                label.font = [UIFont systemFontOfSize:60];
                label.bounds = CGRectMake((date.weekday - 1)*(viewWidth - inter * 4)/7 + inter * 2, (int)(((date.day+[cal weekdayOfFirstDay:date]-2)/7)*(viewHeight - _monthLabel.frame.size.height - inter *9)/5+_monthLabel.frame.size.height + inter *7), (viewWidth - inter * 4)/7-2, (viewHeight - _monthLabel.frame.size.height - inter *9)/5-4);
                label.contentMode = UIViewContentModeCenter;
                label.textAlignment = NSTextAlignmentCenter;
                [dayLabelArray addObject:label];
                [calenderRect addSubview:label];
                
//                MeetingView *meetView = [[NSBundle mainBundle]loadNibNamed:@"MeetingView" owner:self options:nil][date.weekday <= 4];
//                if (date.weekday <= 4)
//                {
//                    meetView.frame = CGRectMake(label.frame.origin.x + label.frame.size.width, label.frame.origin.y + label.frame.size.height/2 - meetView.frame.size.height, meetView.frame.size.width, meetView.frame.size.height);
//                }
//                else
//                {
//                    meetView.frame = CGRectMake(label.frame.origin.x - meetView.frame.size.width, label.frame.origin.y - meetView.frame.size.height + label.frame.size.height/2, meetView.frame.size.width, meetView.frame.size.height);
//                }
//                [calenderRect addSubview:meetView];
//                [meetViews addObject:meetView];
//                meetView.tag = 100 + date.day;
////                [self.view bringSubviewToFront:calenderRect];
//                meetView.myDele = self;
//                meetView.hidden = YES;
            }
        }
            break;
        case 5:
        {
            for (NSString *day in [cal dayInMonth:date])
            {
                date = [NSDate getDateWithString:[NSString stringWithFormat:@"%04d-%02d-%02d",[_yearLabel.text intValue] ,[_monthLabel.text intValue],[day intValue]] DateFormat:@"yyyy-MM-dd"];

                CalenderBtn *button = [[CalenderBtn alloc]initWithFrame:CGRectMake((date.weekday - 1)*(viewWidth - inter * 4)/7 + inter * 2, (((date.day+[cal weekdayOfFirstDay:date]-2)/7)*(viewHeight - _monthLabel.frame.size.height - inter *9)/5 + _monthLabel.frame.size.height + inter*2 + inter *5), (viewWidth - inter * 4)/7-2, (viewHeight - _monthLabel.frame.size.height - inter *9)/5-4)];
                button.backgroundColor = [UIColor clearColor];
                button.tag = [day intValue];
                [calenderRect addSubview:button];
                [dayLabelArray addObject:button];

                UILabel *label = [UILabel new];
                if (date.year == [NSDate date].year && date.month == [NSDate date].month && date.day == [NSDate date].day)
                {
                    label.backgroundColor = [UIColor yellowColor];
                }
                else
                {
                    label.backgroundColor = [UIColor clearColor];
                }
                if (date.weekday == 1 ||date.weekday == 7)
                {
                    label.textColor = [UIColor redColor];
                }
                else
                {
                    label.textColor = [UIColor blackColor];
                }
                label.text = day;
                label.font = [UIFont systemFontOfSize:60];
                label.frame = CGRectMake((date.weekday - 1)*(viewWidth - inter * 4)/7 + inter * 2, (((date.day+[cal weekdayOfFirstDay:date]-2)/7)*(viewHeight - _monthLabel.frame.size.height - inter *9)/5 + _monthLabel.frame.size.height + inter*2 + inter *5), (viewWidth - inter * 4)/7-2, (viewHeight - _monthLabel.frame.size.height - inter *9)/5-4);
                label.contentMode = UIViewContentModeCenter;
                label.textAlignment = NSTextAlignmentCenter;
                [dayLabelArray addObject:label];
                [calenderRect addSubview:label];
                
//                MeetingView *meetView = [[NSBundle mainBundle]loadNibNamed:@"MeetingView" owner:self options:nil][date.weekday <= 4];
//                if (date.weekday <= 4)
//                {
//                    meetView.frame = CGRectMake(label.frame.origin.x + label.frame.size.width, label.frame.origin.y + label.frame.size.height/2 - meetView.frame.size.height, meetView.frame.size.width, meetView.frame.size.height);
//                }
//                else
//                {
//                    meetView.frame = CGRectMake(label.frame.origin.x - meetView.frame.size.width, label.frame.origin.y - meetView.frame.size.height + label.frame.size.height/2, meetView.frame.size.width, meetView.frame.size.height);
//                }
//                [calenderRect addSubview:meetView];
//                [meetViews addObject:meetView];
//                meetView.tag = 100 + date.day;
//                meetView.hidden = YES;
//                meetView.myDele = self;
            }

        }
            break;
        case 6:
        {
            for (NSString *day in [cal dayInMonth:date])
            {
                
                date = [NSDate getDateWithString:[NSString stringWithFormat:@"%04d-%02d-%02d",[_yearLabel.text intValue] ,[_monthLabel.text intValue],[day intValue]] DateFormat:@"yyyy-MM-dd"];

                CalenderBtn *button = [[CalenderBtn alloc]initWithFrame:CGRectMake((date.weekday - 1)*(viewWidth - inter * 4)/7 + inter * 2, (int)(((date.day+[cal weekdayOfFirstDay:date]-2)/7)*(viewHeight - _monthLabel.frame.size.height - inter *9)/6+_monthLabel.frame.size.height + inter *7), (viewWidth - inter * 4)/7-4-2, (viewHeight - _monthLabel.frame.size.height - inter *9)/6)];
                button.backgroundColor = [UIColor clearColor];
                button.tag = [day intValue];
                [calenderRect addSubview:button];
                [dayLabelArray addObject:button];
                
                UILabel *label = [UILabel new];
                if (date.year == [NSDate date].year && date.month == [NSDate date].month && date.day == [NSDate date].day)
                {
                    label.backgroundColor = [UIColor yellowColor];
                }
                else
                {
                    label.backgroundColor = [UIColor clearColor];
                }
                if (date.weekday == 7 ||date.weekday == 1)
                {
                    label.textColor = [UIColor redColor];
                }
                else
                {
                    label.textColor = [UIColor blackColor];
                }
                
                label.text = day;
                label.font = [UIFont systemFontOfSize:60];
                label.frame = CGRectMake((date.weekday - 1)*(viewWidth - inter * 4)/7 + inter * 2, (int)(((date.day+[cal weekdayOfFirstDay:date]-2)/7)*(viewHeight - _monthLabel.frame.size.height - inter *9)/6+_monthLabel.frame.size.height + inter *7), (viewWidth - inter * 4)/7-4-2, (viewHeight - _monthLabel.frame.size.height - inter *9)/6);
                label.contentMode = UIViewContentModeCenter;
                label.textAlignment = NSTextAlignmentCenter;
                [dayLabelArray addObject:label];
                [calenderRect addSubview:label];
//                MeetingView *meetView = [[NSBundle mainBundle]loadNibNamed:@"MeetingView" owner:self options:nil][date.weekday <= 4];
//                if (date.weekday <= 4)
//                {
//                    meetView.frame = CGRectMake(label.frame.origin.x + label.frame.size.width, label.frame.origin.y + label.frame.size.height/2 - meetView.frame.size.height, meetView.frame.size.width, meetView.frame.size.height);
//                }
//                else
//                {
//                    meetView.frame = CGRectMake(label.frame.origin.x - meetView.frame.size.width, label.frame.origin.y - meetView.frame.size.height + label.frame.size.height/2, meetView.frame.size.width, meetView.frame.size.height);
//                }
//                [calenderRect addSubview:meetView];
//                [meetViews addObject:meetView];
//                meetView.tag = 100 + date.day;
////                [self.view bringSubviewToFront:calenderRect];
//                meetView.hidden = YES;
//                meetView.myDele = self;
            }
        }
            break;
    }
}
- (void)showMeetView:(int)tag
{
    NSDate *mydate = [NSDate getDateWithString:[NSString stringWithFormat:@"%04d-%02d-%02d",[_yearLabel.text intValue] ,[_monthLabel.text intValue],tag-100] DateFormat:@"yyyy-MM-dd"];
    MeetingView *meetView = [[NSBundle mainBundle]loadNibNamed:@"MeetingView" owner:self options:nil][mydate.weekday <= 4];
    UIButton *label = (UIButton *)[calenderRect viewWithTag:tag-100];
    if (mydate.weekday <= 4)
    {
        meetView.frame = CGRectMake(label.frame.origin.x + label.frame.size.width, label.frame.origin.y + label.frame.size.height/2 - meetView.frame.size.height, meetView.frame.size.width, meetView.frame.size.height);
    }
    else
    {
        meetView.frame = CGRectMake(label.frame.origin.x - meetView.frame.size.width, label.frame.origin.y - meetView.frame.size.height + label.frame.size.height/2, meetView.frame.size.width, meetView.frame.size.height);
    }
    [calenderRect addSubview:meetView];
    [meetViews addObject:meetView];
    meetView.tag = 100 + mydate.day;
    meetView.myDele = self;

}
#pragma mark - request
- (void)netRequest:(NSString *)dayString
{
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    NSURL *sendUrl = [NSURL URLWithString:checkMeeting];
    ASIFormDataRequest *sendRequest = [ASIFormDataRequest requestWithURL:sendUrl];
    sendRequest.delegate = self;
    if ([nameLabel.text isEqualToString:@"全部"])
    {
        [sendRequest setPostValue:@"" forKey:@"place"];
    }
    else
    {
        [sendRequest setPostValue:[nameLabel.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] forKey:@"place"];
    }
    [sendRequest setPostValue:[NSString stringWithFormat:@"%04i",[_yearLabel.text integerValue]] forKey:@"year"];
    [sendRequest setPostValue:[NSString stringWithFormat:@"%02i",[_monthLabel.text integerValue]] forKey:@"month"];
    [sendRequest setPostValue:dayString forKey:@"day"];
    [sendRequest startAsynchronous];
    if ([dayString isEqualToString:@""])
    {
        [sendRequest setDidFinishSelector:@selector(submitDone:)];
    }
    else
    {
        [sendRequest setDidFinishSelector:@selector(dayDone:)];
    }
    [sendRequest setDidFailSelector:@selector(submitFail)];
    
}
- (void)dayDone:(ASIFormDataRequest *)request
{
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    NSString *response = [request responseString];
    NSDictionary *queryDic = [response JSONValue];
    NSArray *queryDataArr = [queryDic objectForKey:@"data"];
    NSLog(@"查询%@",queryDataArr);
    NSString *dayStr =  [queryDataArr[0][@"eventFromDate"]substringWithRange:NSMakeRange(8, 2)];
    int tag = 100 + [dayStr integerValue];
    [self showMeetView:tag];
    MeetingView *meetView = (MeetingView *)[calenderRect viewWithTag:tag];
    meetView.receiveArr = queryDataArr;
//    int index = mydate.weekday <= 4;
    [meetView.detailViews[0] reloadData];
    [calenderRect bringSubviewToFront:meetView];
    UILabel *titleLb = meetView.titleLbs[0];
    titleLb.text = [NSString stringWithFormat:@"%04d年%02d月%02d日的会议",[_yearLabel.text intValue],[_monthLabel.text intValue],[dayStr intValue]];
}
- (void)submitDone:(ASIFormDataRequest *)obtainFloor
{
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    NSString *response = [obtainFloor responseString];
    NSDictionary *queryDic = [response JSONValue];
    NSArray *queryDataArr = [queryDic objectForKey:@"data"];
    NSLog(@"查询%@",queryDataArr);
    if (queryDataArr.count != 0)
    {
        for (int i = 0; i < queryDataArr.count; i ++)
        {
            NSDictionary *meetingDic = [queryDataArr objectAtIndex:i];
            [eventFromDates addObject:[meetingDic objectForKey:@"eventFromDate"]];
            [eventToDates addObject:[meetingDic objectForKey:@"eventToDate"]];
            [eventNorms addObject:[meetingDic objectForKey:@"eventNorm"]];
        }
        for (int i = 0; i < eventFromDates.count; i++)
        {
            NSString *fromDate = [[eventFromDates objectAtIndex:i]substringFromIndex:8];
            NSString *toDate = [[eventFromDates objectAtIndex:i]substringFromIndex:8];
            NSString *type = [eventNorms objectAtIndex:i];
            for (int j = [fromDate integerValue]; j <= [toDate integerValue]; j ++)
            {
                CalenderBtn *btn = (CalenderBtn *)[calenderRect viewWithTag:j];

                if ([type isEqualToString:@"一般"])
                {
                    btn.greenView.hidden = NO;
                }
                else if ([type isEqualToString:@"特殊"])
                {
                    btn.redView.hidden = NO;
                }
                [calenderRect bringSubviewToFront:btn];
                [btn addTarget:self action:@selector(showDetail:) forControlEvents:UIControlEventTouchUpInside];
            }
        }
    }
}
- (void)submitFail
{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"警告！！" message:@"请求数据失败！！！" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
    [MBProgressHUD hideHUDForView:self.view animated:YES];
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if ([tableView isEqual:yearTable])
    {
        return yearArray.count;
    }
    if ([tableView isEqual:monthTable])
    {
        return monthArray.count;
    }
    if ([tableView isEqual:roomTable]) {
        return places.count;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"myCell";
    if ([tableView isEqual:yearTable]) {
       UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!cell) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        }
        if (cell.textLabel.text == _yearLabel.text) {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.textLabel.text = [NSString stringWithFormat:@"%@年",[yearArray objectAtIndex:indexPath.row]] ;
        [cell setBackgroundColor:[UIColor clearColor]];
        return cell;
    }
    if ([tableView isEqual:monthTable]) {
        UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!cell) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        }
        if (cell.textLabel.text == _monthLabel.text) {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.textLabel.text = [NSString stringWithFormat:@"%@月", [monthArray objectAtIndex:indexPath.row]];
        [cell setBackgroundColor:[UIColor clearColor]];
        return cell;
    }
    if ([tableView isEqual:roomTable]) {
        UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!cell) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        }
        cell.textLabel.text = [places objectAtIndex:indexPath.row];
        cell.textLabel.textColor = [UIColor whiteColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell setBackgroundColor:[UIColor clearColor]];
        return cell;
    }
    return nil;
}
#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   UITableViewCell* cell =[tableView cellForRowAtIndexPath:indexPath];
    if ([tableView isEqual:yearTable])
    {
        _yearLabel.text = [NSString stringWithFormat:@"%@年",[yearArray objectAtIndex:indexPath.row]];
    }
    if ([tableView isEqual:monthTable])
    {
        _monthLabel.text = [NSString stringWithFormat:@"%@月",[monthArray objectAtIndex:indexPath.row]] ;
        monthTable.hidden = YES;
    }
    if ([tableView isEqual:roomTable])
    {
        nameLabel.text = [places objectAtIndex:indexPath.row];
    }
    for (UIView *lb in dayLabelArray) {
        [lb removeFromSuperview];
    }
    [dayLabelArray removeAllObjects];
    
    for (UIView *view in meetViews)
    {
        [view removeFromSuperview];
    }
    [meetViews removeAllObjects];
    
    today = [NSDate getDateWithString:[NSString stringWithFormat:@"%@年-%@月",cell.textLabel.text,_monthLabel.text] DateFormat:@"yyyy年-MM月"];
    [calenderRect setSelectedDate:today] ;
    [calenderRect setNeedsDisplay];
    [eventFromDates removeAllObjects];
    [eventToDates removeAllObjects];
    [eventNorms removeAllObjects];
    [self addCalender];
    [self hiddenAll];
    [self netRequest:@""];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 35;
}

#pragma mark - touch action
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self hiddenAll];
}

#pragma mark - button action
- (void)showDetail:(id)sender
{
    [self hiddenAll];
    [self netRequest:[NSString stringWithFormat:@"%i",[sender tag]]];
}
- (void)yearClick:(UIButton*)btn
{
    if (yearTable.hidden) {
        [self hiddenAll];
        yearTable.hidden = NO;
        [btn setImage:[UIImage imageNamed:@"三角日历下"] forState:UIControlStateNormal];
    }else{
        [self hiddenAll];
    }
}
- (void)monthClick:(UIButton*)btn
{
    if (monthTable.hidden) {
        [self hiddenAll];
        monthTable.hidden = NO;
        [btn setImage:[UIImage imageNamed:@"三角日历下"] forState:UIControlStateNormal];
    }else{
        [self hiddenAll];
    }
}
- (void)room:(UIButton*)btn
{
    if (roomTable.hidden)
    {
        [self hiddenAll];
        roomTable.hidden = NO;
        [btn setImage:[UIImage imageNamed:@"下拉-右框"] forState:UIControlStateNormal];
    }
    else
    {
        [self hiddenAll];
    }
}
- (void)hiddenAll
{
    roomTable.hidden = YES;
    yearTable.hidden = YES;
    monthTable.hidden = YES;
    [topButton setImage:[UIImage imageNamed:@"下拉-右框常态"] forState:UIControlStateNormal];
    [monthBtn setImage:[UIImage imageNamed:@"三角日历右"] forState:UIControlStateNormal];
    [yearBtn setImage:[UIImage imageNamed:@"三角日历右"] forState:UIControlStateNormal];
    for (UIView *view in meetViews)
    {
        [view removeFromSuperview];
    }
    [meetViews removeAllObjects];
    [self.view bringSubviewToFront:roomTable];
    [calenderRect bringSubviewToFront:yearTable];
    [calenderRect bringSubviewToFront:monthTable];
}
- (void)add:(UIButton*)btn
{
    AddMeetViewController *addMeet = [AddMeetViewController new];
    [self.navigationController pushViewController:addMeet animated:YES];
    [self hiddenAll];
}

@end
